<?php
/**
 * User: YL
 * Date: 2019/08/02
 */

require_once __DIR__ . '/vendor/autoload.php';

use restful\utils\Env;
use think\App;
use think\facade\Config;
use think\facade\Db;

// 框架初始化
(new App())->initialize();

// 参数定义
$database = Env::get('database.database');
$options = [
    'adapter' => 'mysql',
    'host' => Env::get('database.hostname'),
    'name' => $database,
    'user' => Env::get('database.username'),
    'pass' => Env::get('database.password'),
    'port' => Env::get('database.hostport'),
    'charset' => Env::get('database.charset'),
    'collation' => Env::get('database.collation'),
    'table_prefix' => Env::get('database.prefix'),
];

// 创建文件
$migrationsPath = __DIR__ . '/db/migrations/';
$seedsPath = __DIR__ . '/db/seeds/';
if (! file_exists($migrationsPath)) {
    mkdir($migrationsPath, 0755, true);
    clearstatcache();
}
if (! file_exists($seedsPath)) {
    mkdir($seedsPath, 0755, true);
    clearstatcache();
}


// 创建数据库
$config = Config::get('database.connections.mysql');
Config::set([
    'connections' => [
        'mysql' => array_merge($config, [
            'database' => 'mysql',
            'prefix' => '',
        ]),
    ],
], 'database');
$sql = sprintf('CREATE DATABASE IF NOT EXISTS `%s` DEFAULT CHARACTER SET `%s` COLLATE `%s`', $database, $options['charset'], $options['collation']);
Db::connect('mysql')->execute($sql);

return [
    'paths' => [
        'migrations' => $migrationsPath,
        'seeds' => $seedsPath,
    ],
    'environments' => [
        'default_migration_table' => 'phinxlog',
        'default_database' => 'local',
        'local' => $options,
    ],
    'version_order' => 'creation',
];